// pages/add-share/index.js
var api = require('../../api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServer: api.default.img_server,

    form: {
    },
    user:{

    },
    img: "/images/img-share-un.png",
    agree: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  inputedit1: function (e) {
    this.data.user.username = e.detail.value;
  },
  inputedit2: function (e) {
    this.data.user.password = e.detail.value;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var store = wx.getStorageSync("store");
    var share_setting = wx.getStorageSync("share_setting");
    page.setData({
      user_info: user_info,
      store: store,
      share_setting: share_setting
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  orderSubmit: function (e) {
    var page = this;
    wx.requestSubscribeMessage({
      tmplIds: ['2Ni3pxgwXhZ-BgC9UgPU-lN-pC5c_95CCsUzHFTFlOo'],
      success(res) {
          page.formSubmit(e);
      }
    })
},
  formSubmit: function (e) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_user = wx.getStorageSync("my_info");
   
    if(my_user.supplyId == undefined || my_user.supplyId ==''){
      wx.showModal({
        content: "请您先申请成为供应商，待审核通过后，方可申请后台管理账号！",
        showCancel: false,
      })
      return;
    }
    page.data.user.supplyId = my_user.supplyId;

  


    if (page.data.user.username == undefined || page.data.user.username == '') {
      wx.showModal({
        content: "有必填项未填写!",
        showCancel: false,
      })
      return;
    }
    if ( page.data.user.username.length<2 ||page.data.user.username.length>8){
      wx.showModal({
        content: "用户名格式错误!",
        showCancel: false,
      })
      return;
    }
    if (page.data.user.password == undefined || page.data.user.password == '') {
      wx.showModal({
        content: "有必填项未填写!",
        showCancel: false,
      })
      return;
    }
    if (page.data.user.password.length < 6 || page.data.user.password.length >20) {
      wx.showModal({
        content: "6-20个字符，字母和数字组合",
        showCancel: false,
      })
      return;
    }
   
  
    app.request({
      url: api.share.create_admin,
      method: 'POST',
      data: page.data.user,
      success: function (res) {
        if (res.code == 200) {
          user_info.is_distributor = 2;
          wx.setStorageSync(
            "user_info", user_info
          );
          wx.showModal({
            title: '',
            content: '您的申请已经提交成功！稍后，平台客服会添加供应商申请表中的微信号告知审核结果！',
            showCancel: false,//是否显示取消按钮
            confirmText: "确定",//默认是“确定”
            success: function (res) {
              if (res.cancel) {
                //点击取消,默认隐藏弹框
              } else {
                //点击确定
                wx.navigateBack({
                  delta: 1
                })
              }
            },
            fail: function (res) { },//接口调用失败的回调函数
            complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
          })
         
        } else {
          wx.showModal({
            content: res.msg,
            showCancel: false,
          })
        }
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  agreement: function () {
    wx.redirectTo({
      url: '/pages/xieyi/xieyi',
    })
    // var share_setting = wx.getStorageSync("share_setting");
    // wx.showModal({
    //   title: '供应商协议',
    //   content: "sdfzzfdfzfrfffg",
    //   showCancel:false,
    //   confirmText:"我已阅读",
    //   confirmColor:"#ff4544",
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     }
    //   }
    // });
  },
  agree: function () {
    var page = this;
    var agree = page.data.agree;
    if (agree == 0) {
      agree = 1;
      page.setData({
        img: "/images/img-share-agree.png",
        agree: agree
      });
    }
    else if (agree == 1) {
      agree = 0;
      page.setData({
        img: "/images/img-share-un.png",
        agree: agree
      });
    }
  },

  chooseImage: function (e) {
    var that = this
    var type = e.currentTarget.dataset.id
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        if (type == 1) {
          that.setData({
            license: tempFilePaths[0]
          })
        } else {
          that.setData({
            cert: tempFilePaths[0]
          })
        }
        that.uploadFileImage(tempFilePaths[0])
      }
    })
  },
  /**
   * 图片上传api调用
   */
  uploadFileImage(img) {
    wx.uploadFile({
      url: api.default.upload_image,//"/api/aliyun/oss/callback",
      filePath: img,
      name: 'file',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res
      }
    })
  }
})